package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.BussinessLevel;
import com.sl.au.entity.M2R;
import com.sl.au.entity.Module;
import com.sl.au.repository.BussinessLevelRepository;
import com.sl.au.service.BussinessLevelService;
import com.sl.au.service.ModuleService;

@Service("BussinessLevelService")
@Transactional
public class BussinessLevelServiceImpl extends BaseServiceImpl implements BussinessLevelService{
	
	@Autowired
	BussinessLevelRepository bussinessLevelRepository;
	
	@Override
	public List<BussinessLevel> getAllNodes() {
		
		List<BussinessLevel> nodes = new ArrayList<BussinessLevel>();
		
		nodes = bussinessLevelRepository.findAll();
		
		return nodes;
	}

	@Override
	public BussinessLevelRepository getRepository() {
		return this.bussinessLevelRepository;
	}

	@Override
	public List<Map<String, Object>> getNodeMap(List<BussinessLevel> node) {
		List<Map<String, Object>> nodeMap = new ArrayList<Map<String, Object>>();
		for (BussinessLevel p : node) {
			if (null != p) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", p.getId());
				map.put("parentId", p.getLevelParentId());
				if (p.getLevelParentId().equals("0")) {
					String Id = p.getId();
					if (StringUtils.hasText(Id)) {
						map.put("name", p.getLevelName());
						//map.put("nocheck", true);
					}
				} else {
					map.put("name", String.valueOf(p.getLevelOrder())+p.getLevelName());
				}
				if (StringUtils.hasText(p.getLevelParentId())) {
					map.put("isParent", p.getLevelParentId());
				}
				/*
				if (StringUtils.hasText(p.getIsOpen())) {
					map.put("open", p.getIsOpen());
				}
				*/
				map.put("open", true);
				nodeMap.add(map);
			}
		}
		return nodeMap;
	}

	
	@Override
	public List<BussinessLevel> getBussinessLevels(int start, int length, String key, String orderName, String dir) {
		Page<BussinessLevel> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "id");
		pages = bussinessLevelRepository.getBussinessLevelName(key, pageable);
		// pages = moduleRepository.findByModuleNameLike(moduleName, pageable);
		return pages.getContent();
	}

	@Override
	public List<BussinessLevel> getPage(int start, int length, String key, String orderName, String dir) {
		Page<BussinessLevel> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "id");
		pages = bussinessLevelRepository.getData(pageable);
		return pages.getContent();
	}

	@Override
	public void deleteNodes(String[] ids) {
		int node;
		if (ids.length > 0) {
			for (String id : ids) {
				if (StringUtils.hasText(id)) {
					
					bussinessLevelRepository.setBussinessLevel(id);
				}
			}
		}
		return;
	}

	@Override
	public BussinessLevel findByLevelName(String levelName) {
	
		return bussinessLevelRepository.findByLevelName(levelName);
	}
}
